package com.school.controller;

import com.alibaba.druid.util.StringUtils;
import com.school.dto.user.InsertStudentUserDTO;
import com.school.model.ResultModel;
import com.school.model.Role;
import com.school.model.User;
import com.school.service.RoleService;
import com.school.tool.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Controller()
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping("")
    public @ResponseBody ResultModel insertRole(@Param("name") String name){
        if(StringUtils.isEmpty(name)){
            return Result.error("角色名不能为空!");
        }
        Role role=new Role();
        role.setName(name);
        int resultCode=roleService.insertSelective(role);
        if(resultCode>0){
            return Result.success();
        }else if(resultCode==-1){
            return Result.error("添加失败，角色名重复！");
        }else{
            return Result.error("添加失败！");
        }
    }

    @PostMapping("/id")
    public @ResponseBody ResultModel updateRole(@Param("id") Integer id,@Param("name")String name){
        if(id==null){
            return Result.error("主键不能为空");
        }
        if(StringUtils.isEmpty(name)){
            return Result.success();
        }
        Role role=new Role();
        role.setId(id);
        role.setName(name);
        int resultCode=roleService.updateByPrimaryKeySelective(role);
        if(resultCode>0){
            return Result.success();
        }else if(resultCode==-1){
            return Result.error("修改失败，角色名重复！");
        }else{
            return Result.error("添加失败！");
        }
    }

}
